(function(){
    "use strict";
app.controller('developTabCtrl', ['$rootScope', '$scope', 'httpLoad', '$modal', '$stateParams', '$state', function ($rootScope, $scope, httpLoad, $modal, $stateParams, $state) {
	 /* 变量声明
    ---------------------------------------*/
	 var flag = $stateParams.flag,
	     id = $stateParams.id;
      $scope.app_Name = $stateParams.appName;
	 
	/* scope变量声明
    ---------------------------------------*/
	$rootScope.moduleTitle = '应用管理 > 应用部署';
    $rootScope.link = '/statics/css/common.css';//引入页面样式
    $scope.isBack = false;
    $scope.isEnv = false;
    $scope.pageNation={}; //最外层分页信息
    
	/* UI事件绑定
    ---------------------------------------*/
    $scope.goBack = goBackFun;
    $scope.getVersionList=getVersionListFun; //获取版本列表（分页用到）
    $scope.getContainerList=getContainerListFun;  //获取容器列表 （分页用到）
    
	/* 入口函数 （代码执行的唯一入口，也是起始点，位置可以根据具体情况调整）
    ---------------------------------------*/
   (function () {
       _init();
   })();

   /* UI事件实现
    ---------------------------------------*/
   function goBackFun(){
       if(flag==0){
           $state.go('app.application.develop');
       }else if(flag==1){
           $state.go('app.containerManger.img');
       }else if(flag==2){
           $state.go('app.containerManger.net');
       }else if(flag==3){
           $state.go('app.containerManger.disk');
       }
   }
   
   function getVersionListFun(page){
	   _getAppVersion({appId:id,rows:5,page:page});
   }
   function getContainerListFun(father,page){
	   _getContByVersion({
			appName:$stateParams.appCoreRename,
			rows:5,
			page:page,
			imageName:father.configImageName+':'+father.configImageTag
		},father);
   }
   
   /* 内部方法实现
   ---------------------------------------*/
	function _init(){
		 var flag_develop = sessionStorage.getItem('developtabLocation')||1;
		if(flag==null){
	        $scope.isBack = false;
	    }else{
	        $scope.isBack = true;
	    }
		//_getAppDetail(id);
		_getAppVersion({appId:id,rows:5,page:1});
		switch (flag_develop/1){
	        case 1:
	            $scope.isAppview = true;
	            break;
	        case 2:
	            $scope.isService = true;
	            break;
	        case 3:
	            $scope.isAppLog = true;
	            break;
	        case 4:
	            $scope.isMonitor = true;
	            break;
	        case 5:
	            $scope.isChoreography = true;
	            break;
	        case 6:
	            $scope.isConfig = true;
	            break;
	        case 7:
	            $scope.isShen = true;
	            break;
	        case 8:
	            $scope.isChangeDeploy = true;
	            break;
	        case 9:
	            $scope.isOperation = true;
	            break;
	        case 10:
	            $scope.iskeepalive = true;
	            break;
	    }
	}
	//获取应用详情
	function _getAppDetail(appId){
		httpLoad.loadData({
            url:'/application/getApplicationDetails.do',
            data: { 
            	'applicationCore.applicationCoreId' : appId
            },
            success:function(rep){
                if(rep.success&&rep.rows){
                   
                }
            }
        });
	}
	//获取版本列表
	function _getAppVersion(obj){
		var len,i;
		httpLoad.loadData({
            url:'/application/getApplicationImageVersion.do',
            data: { 
            	'applicationCore.applicationCoreId' : obj.appId,
            	rows:obj.rows,
            	page:obj.page
            },
            success:function(rep){
                if(rep.success&&rep.rows){
                	$scope.versionAllList=rep.rows;
                	len=$scope.versionAllList.length;
                	$scope.pageNation.total=rep.records;
                	$scope.pageNation.rowtotal=len;
                	$scope.pageNation.page=obj.page;
                	$scope.pageNation.rows=obj.rows;
                	
                	if(len>0){
                		for(i=0;i<len;i++){
                			_getContByVersion({
                				appName:$stateParams.appCoreRename,
                				rows:5,
                				page:1,
                				imageName:$scope.versionAllList[i].configImageName+':'+$scope.versionAllList[i].configImageTag
                			},$scope.versionAllList[i]);
                		}
                	}
                }
            }
        });
	}
	function _getContByVersion(obj,father){
		httpLoad.loadData({
            url:'/deployer/getContainersFromAppName.do',
            data: { 
            	appName : obj.appName,
            	rows:obj.rows,
            	page:obj.page,
            	imageName:obj.imageName
            },
            success:function(rep){
                if(rep.success&&rep.rows){
            		father.conAllList=rep.rows;
            		father.childrenNum=rep.records;
            		father.childrenRows=obj.rows;
            		father.childrenPage=obj.page;
                }
            }
        });
	}
}]);
app.controller('appviewCtrl', ['$rootScope', '$scope', 'httpLoad', '$modal', '$stateParams', '$state','$window',function ($rootScope, $scope, httpLoad, $modal, $stateParams, $state,$window) {
    $scope.basicInfo = function(){
        httpLoad.loadData({
            url:'/application/getApplicationDetails.do',
            data: {"applicationCore.applicationCoreId":$stateParams.id},
            success:function(data){
                $scope.versionCount = data.versionCounts;
                $scope.runningCount = data.runningCounts;
                $scope.notRunningCount = data.notRunningCounts;
                $scope.networkCount = data.networkCounts;
            }
        });
        httpLoad.loadData({
            url:'/runtime/image/getUndeployImagesByProjectId',
            data: {projectId:$stateParams.id},
            success:function(data){
                if(data.success&&data.data.length!=0){
                    $scope.imglength = data.data.length;
                    $scope.isImageLength = false;
                }else if(data.success&&data.data.length==0){
                    $scope.isImageLength = true;
                }else{
                    $scope.pop(data.message,'error');
                    $scope.isImageLength = true;
                }
            }
        });
    };
    $scope.basicInfo();
/*    //获取主机列表
    $scope.getVersionList = function(v_page){
        $scope.param = {
            v_page:1,
            v_rows: 1,
        };
        $scope.param.v_page = v_page || $scope.param.v_page;
        var params = {
            v_page: $scope.param.v_page,
            v_rows: $scope.param.v_rows,
            "applicationCore.applicationCoreId":$stateParams.id
        };
        httpLoad.loadData({
            url:'/application/getApplicationImageVersion.do',
            data: params,
            success:function(data){
                if(data.rows&&data.rows.length!=0){
                    $scope.versionAllList = data.rows;
                    var length = $scope.versionAllList.length;
                    for(var i = 0; i < length; i++){
                        var a = 1111;
                        $scope.getFun = function(){
                            var b = 2222;
                        httpLoad.loadData({
                            url:'/application/getContainersFromAppName.do',
                            async: false,
                            data: {  page:1,
                                     rows:10,
                                     appName:$stateParams.appCoreRename,
                                     imageName:$scope.versionAllList[i].versionName,
                            } ,
                            success:function(dataRow){
                                if(dataRow.rows&&dataRow.rows.length!=0){
                                    $scope.containerList = dataRow.rows;
                                    $scope.versionAllList[i]['conAllList'] = $scope.containerList;
                                    $scope.totalCount = dataRow.total;
                                    $scope.versionAllList[i]["conTotalCount"] = $scope.totalCount;
                                }
                            }
                        });
                        }
                        $scope.getFun();
                    }
                    $scope.totalCount = data.total;
                    $scope.isImageData = false;
                }else{
                    $scope.isImageData = true;
                }
            }
        });
    };
    $scope.getVersionList(1);*/

    //容器详情页
    $scope.detail = function(item){
        $state.go('app.containerManger.containerDetail',{id:item.instanceCoreId,conName:item.instanceRename,flag:3});
    };
    //跳转镜像
    $scope.imgList = function(item){
        $state.go('app.containerManger.img',{id:item.id,conname : item.name,flag:2});
    };
    $scope.peopleBu = function(){
        var modalInstance = $modal.open({
            templateUrl : '/statics/tpl/application/develop/manualDeployment.html',
            controller : 'manualDeploymentModalCtrl',// 初始化模态范围
            resolve : {
                appName: function() {
                    return $stateParams.appName;
                }
            }
        });
        modalInstance.result.then(function(){
            $window.location.reload();
        },function(){});
    }
    $scope.autoBu = function(){
        var modalInstance = $modal.open({
            templateUrl : '/statics/tpl/application/develop/autoDeployment.html',
            controller : 'autoDeploymentModalCtrl'// 初始化模态范围
        });
        modalInstance.result.then(function(data){
            $scope.getData();
        },function(){});
    }
    $scope.capacity = function(){
        var modalInstance = $modal.open({
            templateUrl : '/statics/tpl/application/develop/capacity.html',
            controller : 'capacityModalCtrl'// 初始化模态范围
        });
        modalInstance.result.then(function(){
            $window.location.reload();
        },function(){});
    }
    $scope.reduce = function(){
        var modalInstance = $modal.open({
            templateUrl : '/statics/tpl/application/develop/reduce.html',
            controller : 'reduceModalCtrl'// 初始化模态范围
        });
        modalInstance.result.then(function(){
            $window.location.reload();
        },function(){});
    }
    $scope.reback = function(){
        var modalInstance = $modal.open({
            templateUrl : '/statics/tpl/application/develop/reback.html',
            controller : 'rebackModalCtrl'// 初始化模态范围
        });
        modalInstance.result.then(function(){
            $window.location.reload();
        },function(){});
    }
    
    $scope.showLog = function(item){
        // var modalInstance,str="/abcs/data/local/localFileSystem/"+item.appName+'/'+item.instanceRename;
        var modalInstance,str=item.instanceRename;
        	modalInstance= $modal.open({
            templateUrl : '/statics/tpl/application/develop/showLog.html',  //指向上面创建的视图
            controller : 'showLogModalCtrl',// 初始化模态范围
            windowClass: 'log-win',
            resolve : {
                id: function() {
                    return str;
                }
            }
        });
        modalInstance.result.then(function(data){
            angular.extend(item,data);
        },function(){});
    };
    $scope.showMonitor = function(item){
        var modalInstance = $modal.open({
            templateUrl : '/statics/tpl/application/develop/showMonitor.html',  //指向上面创建的视图
            controller : 'showMonitorModalCtrl',// 初始化模态范围
            resolve : {
                id: function() {
                    return item.id;
                }
            }
        });
        modalInstance.result.then(function(data){
            angular.extend(item,data);
        },function(){});
    };
    $scope.showTerminal = function(item){
        var modalInstance = $modal.open({
            templateUrl : '/statics/tpl/application/develop/showTerminal.html',  //指向上面创建的视图
            controller : 'showTerminalModalCtrl',// 初始化模态范围
            resolve : {
                id: function() {
                    return item.id;
                }
            }
        });
        modalInstance.result.then(function(data){
            angular.extend(item,data);
        },function(){});
    };
}])
app.controller('serviceCtrl', ['$rootScope', '$scope', 'httpLoad', '$modal', '$stateParams', '$state',function ($rootScope, $scope, httpLoad, $modal, $stateParams, $state) {
    //获取
    $scope.serviceFun = function () {

    };
}])
 //日志ctrl
angular.module('app').controller('logModalCtrl',['$scope','$modalInstance','id','LANGUAGE','httpLoad',
function($scope,$modalInstance,id,LANGUAGE,httpLoad){ //依赖于modalInstance
		console.log(id);
        /*httpLoad.loadData({
            url:'/container/log',
            method:'POST',
            data:{id: id},
            success:function(data){
                if(data.success){
                    $scope.codeMirror.setValue(data.message);
                }else{
                    $scope.pop(data.message);
                }
            }
        });*/
    $scope.cancel = function(){
        $modalInstance.dismiss('cancel'); // 退出
    }
}]);
app.controller('appLogCtrl', ['$rootScope', '$scope', 'httpLoad', '$modal', '$stateParams', '$state','meterService','$sce',function ($rootScope, $scope, httpLoad, $modal, $stateParams, $state,meterService,$sce) {
    //获取
    $scope.appLogFun = function () {
        httpLoad.loadData({
            url:'/logger/logger',
            data: {
                key : $stateParams.appCoreRename,
                // key : "/abcs/data/local/localFileSystem/"+$stateParams.appCoreRename,
            },
            success:function(rep){
                $scope.logUrl=$sce.trustAsResourceUrl(rep.url);
            }
        });

    };
}])
app.controller('monitorCtrl', ['$rootScope', '$scope', 'httpLoad', '$modal', '$stateParams', '$state',function ($rootScope, $scope, httpLoad, $modal, $stateParams, $state) {
    $scope.monitorFun = function () {

    };
}])
app.controller('choreographyCtrl', ['$rootScope', '$scope', 'httpLoad', '$modal', '$stateParams', '$state',function ($rootScope, $scope, httpLoad, $modal, $stateParams, $state) {
    $scope.choreographyFun = function () {

    };
}])
app.controller('configCtrl', ['$rootScope', '$scope', 'httpLoad', '$modal', '$stateParams', '$state',function ($rootScope, $scope, httpLoad, $modal, $stateParams, $state) {
    $scope.configFun = function () {
         $scope.param = {

         };
        httpLoad.loadData({
            url:'/application/getApplicationConfigVersion.do',
            data: {"applicationCore.applicationCoreId":$stateParams.id},
            success:function(data){
                if(data.rows.length!=0){
                    $scope.imgTypes = data.rows;
                    $scope.imgAll.imgType = $scope.imgTypes[0].value;
                    httpLoad.loadData({
                        url:'/application/getOneApplicationConfigInfo.do',
                        data: {"air.configCoreId":$scope.imgTypes[0].value},
                        success:function(data){
                            $scope.rowEnvList = data.configEnvsList;
                            $scope.rowDiskList = data.configVolumesList;
                            $scope.rowPortList = data.configPortsList;
                            $scope.param.cpu = data.configBasic.configBasicCpu;
                            $scope.param.mem = data.configBasic.configBasicMemory;
                        }
                    });
                }
            }
        });
    };
    $scope.imgAll = {};
    $scope.change = function(x){
        httpLoad.loadData({
            url:'/application/getOneApplicationConfigInfo.do',
            data: {"applicationCore.applicationCoreId":$stateParams.id,
                   "air.configCoreId":x},
            success:function(data){
                $scope.rowEnvList = data.configEnvsList;
                $scope.rowDiskList = data.configVolumesList;
                $scope.rowPortList = data.configPortsList;
                $scope.param.cpu = data.configBasic.configBasicCpu;
                $scope.param.mem = data.configBasic.configBasicMemory;
            }
        });
    }
    $scope.env ={};
    $scope.disk ={};
    $scope.port ={};
    function  ObjStoryEnv(configEnvsName,configEnvsValue) //声明对象
    {
        this.configEnvsName = configEnvsName;
        this.configEnvsValue= configEnvsValue;
    }
    function  ObjStoryDisk(configVolumesPath) //声明对象
    {
        this.configVolumesPath= configVolumesPath;
    }
    function  ObjStoryPort(configPortsPort) //声明对象
    {
        this.configPortsPort= configPortsPort;
    }
    $scope.addEnv = function(){
        var length = $scope.rowEnvList.length;
        for(var i = 0; i < length; i++){
            if($scope.rowEnvList[i].configEnvsName == $scope.env.key)
            {
                $scope.pop("Key已存在",'error');
                return;
            }
        }
        $scope.rowEnvList.push(new ObjStoryEnv($scope.env.key,$scope.env.value));
    }
    $scope.removeEnv = function(row){
        var length = $scope.rowEnvList.length;
        for(var i = 0; i < length; i++)
        {
            if($scope.rowEnvList[i] == row)
            {
                $scope.rowEnvList.splice(i,1); //删除下标为i的元素
                return;
            }
        }
    }
    $scope.addDisk = function(){
        var length = $scope.rowDiskList.length;
        for(var i = 0; i < length; i++){
            if($scope.rowDiskList[i].configVolumesPath == $scope.disk.path)
            {
                $scope.pop("path已存在",'error');
                return;
            }
        }
        $scope.rowDiskList.push(new ObjStoryDisk($scope.disk.path));
    }
    $scope.removeDisk = function(row){
        var length = $scope.rowDiskList.length;
        for(var i = 0; i < length; i++)
        {
            if($scope.rowDiskList[i] == row)
            {
                $scope.rowDiskList.splice(i,1); //删除下标为i的元素
                return;
            }
        }
    }
    $scope.addPort = function(){
        var length = $scope.rowPortList.length;
        for(var i = 0; i < length; i++){
            if($scope.rowPortList[i].configPortsPort == $scope.port.value)
            {
                $scope.pop("端口已存在",'error');
                return;
            }
        }
        $scope.rowPortList.push(new ObjStoryPort($scope.port.value));
    }
    $scope.removePort = function(row){
        var length = $scope.rowPortList.length;
        for(var i = 0; i < length; i++)
        {
            if($scope.rowPortList[i] == row)
            {
                $scope.rowPortList.splice(i,1); //删除下标为i的元素
                return;
            }
        }
    }
    $scope.updataResource = function(){
        $scope.envList="";
        for(var i = 0; i < $scope.rowEnvList.length; i++)
        {
            $scope.envList+=$scope.rowEnvList[i].configEnvsName+":"+$scope.rowEnvList[i].configEnvsValue+";"
        }
        $scope.diskList="";
        for(var i = 0; i < $scope.rowDiskList.length; i++)
        {
            // $scope.diskList+=$scope.rowDiskList[i].configVolumesType+":"+$scope.rowDiskList[i].configVolumesName+":"+$scope.rowDiskList[i].configVolumesPath+";"
            $scope.diskList+="local:local-Name"+":"+$scope.rowDiskList[i].configVolumesPath+":localFileSystem"+";"
        }
        $scope.portList="";
        for(var i = 0; i < $scope.rowPortList.length; i++)
        {
            $scope.portList+=$scope.rowPortList[i].configPortsPort+";"
        }
        httpLoad.loadData({
            url:'/application/addConfigVersion.do',
            data: {"applicationCore.applicationCoreId":$stateParams.id,
                "configBasic.configBasicCpu":$scope.param.cpu,
                "configBasic.configBasicMemory":$scope.param.mem,
                configPortsListString:$scope.portList,
                configEnvsListString:$scope.envList,
                configVolumesListString:$scope.diskList,
                "cc.configCoreId":$scope.imgAll.imgType,
                "configBasic.configBasicNetwork": "network1"},
            success:function(data){
                if(data.success){
                    $scope.pop(data.message);
                }else{
                    $scope.pop(data.message,"error");
                }
            }
        });
    }
}])
app.controller('operationCtrl', ['$rootScope', '$scope', 'httpLoad', '$modal', '$stateParams', '$state',function ($rootScope, $scope, httpLoad, $modal, $stateParams, $state) {
        $scope.operationFun = function (page) {
            $scope.param = {
                rows: 10
            };
            $scope.isbatchDelete = true;
            $scope.getData = function(page){
                $scope.param.page = page || $scope.param.page;
                var params = {
                    page: $scope.param.page,
                    rows: $scope.param.rows,
                    applicationCoreId:$stateParams.id
                };
                httpLoad.loadData({
                    url:'/application/getApplicationEvents.do',
                    data: params,
                    success:function(data){
                        if(data.rows&&data.rows.length!=0){
                            $scope.eventList = data.rows;
                            $scope.totalCount = data.total;
                            $scope.isImageData = false;
                        }else{
                            $scope.isImageData = true;
                        }
                    }
                });
            };
            $scope.getData(1);
        };
    }])
app.controller('shenCtrl', ['$rootScope', '$scope', 'httpLoad', '$modal', '$stateParams', '$state',function ($rootScope, $scope, httpLoad, $modal, $stateParams, $state) {
    //获取主机列表
    $scope.shenFun = function(page){

    };
}])
app.controller('changeDeployCtrl', ['$rootScope', '$scope', 'httpLoad', '$modal', '$stateParams', '$state',function ($rootScope, $scope, httpLoad, $modal, $stateParams, $state) {
    $scope.changeDeployFun = function(){
        httpLoad.loadData({
            url:'/application/getApplicationImageVersion.do',
            data: {"applicationCore.applicationCoreId":$stateParams.id,
                page:1,
                rows:5
            },
            success:function(data){
                $scope.imgsCurrent = data.rows;
                $scope.imgsOld = data.rows;
            }
        });
    };
    $scope.changeDeployFun();
    $scope.huiValue = {
        "applicationCore.applicationCoreId":$stateParams.id,
    };
    $scope.paramn = {

    };
    $scope.paramf = {

    };
    var flag_tab = sessionStorage.getItem('tagType')||1;
    switch (flag_tab/1){
        case 1:
            $scope.isNumber = true;
            break;
        case 2:
            $scope.isFactor = true;
            break;
    };
    sessionStorage.setItem('tagType','1');
    $scope.isFlag=1;
    $scope.checkValue = function(val){
        if(val=="1"){
            $scope.isFlag=1;
        }
        if(val=="2"){
            $scope.isFlag=2;
        }
    };
    httpLoad.loadData({
        url:'/clusterEnv/getClusterByEnvId',
        data: {},
        success:function(data){
            if(data.success){
                $scope.ip = data.data.clusterServiceIp;
                $scope.huiValue.ip = $scope.ip;
            }else{
                $scope.pop("获取IP失败","error");
            }
        }
    });
    $scope.updataHui = function(){
        if($scope.isFlag=="1"){
            $scope.huiValue.conversionType = "number";
            $scope.huiValue.number = $scope.paramn.number;
        };
        if($scope.isFlag=="2"){
            $scope.huiValue.conversionType = "scale";
            $scope.huiValue.factor = $scope.paramf.factor;
        };
        httpLoad.loadData({
            url:'/application/changeDeployer.do',
            data: $scope.huiValue,
            success:function(data){
                if(data.success){
                    $scope.pop(data.message);
                }else{
                    $scope.pop(data.message);
                }
            }
        });
    }
}])
// app.controller('keepaliveCtrl', ['$rootScope', '$scope', 'httpLoad', '$modal', '$stateParams', '$state',function ($rootScope, $scope, httpLoad, $modal, $stateParams, $state) {
//
//     }])
app.controller('manualDeploymentModalCtrl', ['$rootScope', '$scope', 'httpLoad', '$modal', '$stateParams', '$modalInstance','$state','appName',function ($rootScope, $scope, httpLoad, $modal, $stateParams,$modalInstance, $state,appName) {
    $scope.app_name = appName;
    $scope.isadvanced = true;
    $scope.showGao = function($event){
        $scope.isadvanced = false;
        $($event.target).hide();
        $($event.target).next().show();
    }
    $scope.hideGao = function($event){
        $scope.isadvanced = true;
        $($event.target).hide();
        $($event.target).prev().show();
    }
    $scope.rowEnvList = [];
    $scope.rowDiskList = [];
    $scope.rowPortList =  [];
    $scope.isPei = false;
    httpLoad.loadData({
        url:'/application/getApplicationConfigVersion.do',
        data: {"applicationCore.applicationCoreId":$stateParams.id},
        success:function(data){
            if(data.rows.length!=0){
                $scope.busites = data.rows;
                $scope.buImgType = $scope.busites[0].value;
                $scope.isPei = true;
                 httpLoad.loadData({
                     url:'/application/getOneApplicationConfigInfo.do',
                     data: {"air.configCoreId":$scope.busites[0].value},
                     success:function(data){
                         $scope.rowEnvList = data.configEnvsList;
                         $scope.rowDiskList = data.configVolumesList;
                         $scope.rowPortList = data.configPortsList;
                         $scope.paramConfig.cpu = data.configBasic.configBasicCpu;
                         $scope.paramConfig.mem = data.configBasic.configBasicMemory;
                     }
                 });
            }else{
            }
        }
    });

    httpLoad.loadData({
        url:'/runtime/image/getUndeployImagesByProjectId',
        data: {projectId:$stateParams.id},
        success:function(data){
            if(data.data.length!=0){
                $scope.imgs = data.data;
            }
        }
    });
    httpLoad.loadData({
        url:'/clusterEnv/getClusterByEnvId',
        data: {},
        success:function(data){
            if(data.success){
                $scope.ip = data.data.clusterServiceIp;
            }
        }
    });

    $scope.env ={};
    $scope.disk ={};
    $scope.port ={};
    $scope.change = function(x){
        httpLoad.loadData({
            url:'/application/getOneApplicationConfigInfo.do',
            data: {"air.configCoreId":x},
            success:function(data){
                $scope.rowEnvList = data.configEnvsList;
                $scope.rowDiskList = data.configVolumesList;
                $scope.rowPortList = data.configPortsList;
                $scope.paramConfig.cpu = data.configBasic.configBasicCpu;
                $scope.paramConfig.mem = data.configBasic.configBasicMemory;
            }
        });
    }
    function  ObjStoryEnv(configEnvsName,configEnvsValue) //声明对象
    {
        this.configEnvsName = configEnvsName;
        this.configEnvsValue= configEnvsValue;
    }
    function  ObjStoryDisk(configVolumesPath) //声明对象
    {
        this.configVolumesPath= configVolumesPath;
    }
    function  ObjStoryPort(configPortsPort) //声明对象
    {
        this.configPortsPort= configPortsPort;
    }
    $scope.addEnv = function(){
        if($scope.rowEnvList.length>0){
            var length = $scope.rowEnvList.length;
            for(var i = 0; i < length; i++){
                if($scope.rowEnvList[i].configEnvsName == $scope.env.key)
                {
                    $scope.pop("Key已存在",'error');
                    return;
                }
            }
        }
        $scope.rowEnvList.push(new ObjStoryEnv($scope.env.key,$scope.env.value));
    }
    $scope.removeEnv = function(row){
        var length = $scope.rowEnvList.length;
        for(var i = 0; i < length; i++)
        {
            if($scope.rowEnvList[i] == row)
            {
                $scope.rowEnvList.splice(i,1); //删除下标为i的元素
                return;
            }
        }
    }
    $scope.addDisk = function(){
        if($scope.rowDiskList.length>0){
            var length = $scope.rowDiskList.length;
            for(var i = 0; i < length; i++){
                if($scope.rowDiskList[i].configVolumesPath == $scope.disk.path)
                {
                    $scope.pop("path已存在",'error');
                    return;
                }
            }
        }
        $scope.rowDiskList.push(new ObjStoryDisk($scope.disk.path));
    }
    $scope.removeDisk = function(row){
        var length = $scope.rowDiskList.length;
        for(var i = 0; i < length; i++)
        {
            if($scope.rowDiskList[i] == row)
            {
                $scope.rowDiskList.splice(i,1); //删除下标为i的元素
                return;
            }
        }
    }
    $scope.addPort = function(){
        if($scope.rowPortList.length>0){
            var length = $scope.rowPortList.length;
            for(var i = 0; i < length; i++){
                if($scope.rowPortList[i].configPortsPort == $scope.port.value)
                {
                    $scope.pop("端口已存在",'error');
                    return;
                }
            }
        }
        $scope.rowPortList.push(new ObjStoryPort($scope.port.value));
    }
    $scope.removePort = function(row){
        var length = $scope.rowPortList.length;
        for(var i = 0; i < length; i++)
        {
            if($scope.rowPortList[i] == row)
            {
                $scope.rowPortList.splice(i,1); //删除下标为i的元素
                return;
            }
        }
    }
    $scope.paramConfig = {

    };
    $scope.ok = function(){
        $scope.envList="";
        $scope.diskList="";
        $scope.portList="";
        for(var i = 0; i < $scope.rowEnvList.length; i++)
        {
            $scope.envList+=$scope.rowEnvList[i].configEnvsName+":"+$scope.rowEnvList[i].configEnvsValue+";"
        }

        for(var i = 0; i < $scope.rowDiskList.length; i++)
        {
            $scope.diskList+="local:local-Name"+":"+$scope.rowDiskList[i].configVolumesPath+":"+"localFileSystem"+";"
        }

        for(var i = 0; i < $scope.rowPortList.length; i++)
        {
            $scope.portList+=$scope.rowPortList[i].configPortsPort+";"
        }
        if($scope.isPei){
          $scope.addDate = {
              "applicationCore.applicationCoreId":$stateParams.id,
              ip:$scope.ip,
              "instanceImage.configImageName":$scope.img.substring(0,$scope.img.lastIndexOf(":")),
              "instanceImage.configImageTag":$scope.img.substring($scope.img.lastIndexOf(":")+1),
              "configBasic.configBasicCpu":$scope.paramConfig.cpu,
              "configBasic.configBasicMemory":$scope.paramConfig.mem,
              "configBasic.configBasicNetwork":"network1",
              configPortsListString:$scope.portList,
              configEnvsListString:$scope.envList,
              configVolumesListString:$scope.diskList,
              instanceReplica:$scope.instanceReplica,
              "configCore.configCoreId": $scope.buImgType
           }
        }else{
            $scope.addDate = {
                "applicationCore.applicationCoreId":$stateParams.id,
                ip:$scope.ip,
                "instanceImage.configImageName":$scope.img.substring(0,$scope.img.lastIndexOf(":")),
                "instanceImage.configImageTag":$scope.img.substring($scope.img.lastIndexOf(":")+1),
                "configBasic.configBasicCpu":$scope.paramConfig.cpu,
                "configBasic.configBasicMemory":$scope.paramConfig.mem,
                "configBasic.configBasicNetwork":"network1",
                configPortsListString:$scope.portList,
                configEnvsListString:$scope.envList,
                configVolumesListString:$scope.diskList,
                instanceReplica:$scope.instanceReplica,
            }
        };
        httpLoad.loadData({
            url:'/application/constructApplicationConfig.do',
            data: $scope.addDate,
            success:function(data){
                if(data.success){
                    $scope.pop("部署成功");
                    httpLoad.loadData({
                        url:'/runtime/image/modify',
                        data: {
                            imageId:$('#selectImg option:selected').attr('id'),
                            deployStatus:1
                        },
                        success:function(data){
                            if(data.success){
                                $modalInstance.close();
                            }else{
                                $scope.pop(data.message);
                            }
                        }
                    });
                }else{
                    $scope.pop(data.message);
                }
            }
        });
    }
    $scope.cancel = function(){
        $modalInstance.dismiss('cancel'); // 退出
    };
    }])
app.controller('autoDeploymentModalCtrl', ['$rootScope', '$scope', 'httpLoad', '$modal', '$stateParams', '$modalInstance', '$state',function ($rootScope, $scope, httpLoad, $modal, $stateParams, $modalInstance,$state) {
    $scope.cancel = function(){
        $modalInstance.dismiss('cancel'); // 退出
    };
    }])
angular.module('app').controller('capacityModalCtrl',['$scope','$modalInstance','LANGUAGE','httpLoad','$stateParams','$state',
        function($scope,$modalInstance,LANGUAGE,httpLoad,$stateParams,$state){ //依赖于modalInstance
            httpLoad.loadData({
                url:'/application/getApplicationImageVersion.do',
                data: {"applicationCore.applicationCoreId":$stateParams.id,
                    page:1,
                    rows:5
                },
                success:function(data){
                    $scope.kuosites = data.rows;
                }
            });
            $scope.addData = {

            };
            httpLoad.loadData({
                url:'/clusterEnv/getClusterByEnvId',
                data: {},
                success:function(data){
                    if(data.success){
                        $scope.ip = data.data.clusterServiceIp;
                        $scope.addData = {
                            policyIndex : 0,
                            "applicationCore.applicationCoreId" : $stateParams.id,
                            ip : $scope.ip,
                        }
                    }else{
                        $scope.pop("获取IP失败","error");
                    }
                }
            });

            $scope.ok = function(){
                httpLoad.loadData({
                    url:'/application/changePolicy.do',
                    data: $scope.addData,
                    success:function(data){
                        if(data.success){
                            $scope.pop(data.message);
                            $modalInstance.close();
                        }
                    }
                });
            };
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            }
        }]);
angular.module('app').controller('reduceModalCtrl',['$scope','$modalInstance','LANGUAGE','httpLoad','$stateParams',
        function($scope,$modalInstance,LANGUAGE,httpLoad,$stateParams){ //依赖于modalInstance
            httpLoad.loadData({
                url:'/application/getApplicationImageVersion.do',
                data: {"applicationCore.applicationCoreId":$stateParams.id,
                    page:1,
                    rows:5
                },
                success:function(data){
                    $scope.suosites = data.rows;
                }
            });
            $scope.addData = {
                policyIndex : 1,
                "applicationCore.applicationCoreId" : $stateParams.id,
            }
            $scope.ok = function(){
                httpLoad.loadData({
                    url:'/application/changePolicy.do',
                    data: $scope.addData,
                    success:function(data){
                        if(data.success){
                            $scope.pop(data.message);
                            $modalInstance.close($scope.addData);
                        }
                    }
                });
            };
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            }
        }]);
angular.module('app').controller('rebackModalCtrl',['$scope','$modalInstance','LANGUAGE','httpLoad','$stateParams',
        function($scope,$modalInstance,LANGUAGE,httpLoad,$stateParams){ //依赖于modalInstance
            httpLoad.loadData({
                url:'/application/getApplicationImageVersion.do',
                data: {"applicationCore.applicationCoreId":$stateParams.id,
                       page:1,
                       rows:5,
                },
                success:function(data){
                    $scope.backsites = data.rows;
                    $scope.backsitesBack = data.rows;
                }
            });
            $scope.addData = {

            }
            httpLoad.loadData({
                url:'/clusterEnv/getClusterByEnvId',
                data: {},
                success:function(data){
                    if(data.success){
                        $scope.ip = data.data.clusterServiceIp;
                        $scope.addData = {
                            "applicationCore.applicationCoreId" : $stateParams.id,
                            conversionType : "scale",
                            factor : 1,
                            ip : $scope.ip,
                        }
                    }else{
                        $scope.pop("获取IP失败","error");
                    }
                }
            });

            $scope.ok = function(){
                if($scope.addData.currentVersion==$scope.addData.oldVersion){
                    $scope.pop("请选择不同版本","error");
                }
                httpLoad.loadData({
                    url:'/application/changeDeployer.do',
                    data: $scope.addData,
                    success:function(data){
                        if(data.success){
                            $scope.pop(data.message);
                            $modalInstance.close($scope.addData);
                        }else{
                            $scope.pop(data.message);
                        }
                    }
                });
            };
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            }
        }]);
angular.module('app').controller('showLogModalCtrl',['$scope','$modalInstance','id','LANGUAGE','httpLoad','$sce',
        function($scope,$modalInstance,id,LANGUAGE,httpLoad,$sce){ //依赖于modalInstance
            httpLoad.loadData({
                url:'/logger/logger',
                method:'POST',
                data:{key: id},
                success:function(rep){
                    if(rep.status==200){
                    	$scope.logUrl=rep.url;
                    	$scope.logUrl=$sce.trustAsResourceUrl(rep.url);
                    }else{
                        $scope.pop(data.message);
                    }
                }
            });
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            }
        }]);
angular.module('app').controller('showMonitorModalCtrl',['$scope','$modalInstance','id','LANGUAGE','httpLoad','meterService',
        function($scope,$modalInstance,id,LANGUAGE,httpLoad,meterService){ //依赖于modalInstance
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            }
        }]);
angular.module('app').controller('showTerminalModalCtrl',['$scope','$modalInstance','id','LANGUAGE','httpLoad',
        function($scope,$modalInstance,id,LANGUAGE,httpLoad){ //依赖于modalInstance
            httpLoad.loadData({
                url:'/container/log',
                method:'POST',
                data:{id: id},
                success:function(data){
                    if(data.success){
                        $scope.codeMirror.setValue(data.message);
                    }else{
                        $scope.pop(data.message);
                    }
                }
            });
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            }
        }]);
//本地上传
angular.module('app').controller('uploadModalCtrl',['$scope','$modalInstance','httpLoad','LANGUAGE',
    function($scope,$modalInstance,httpLoad,LANGUAGE){ //依赖于modalInstance
        $scope.websocketUrl='/uploadService';
        $scope.cancel = function(){
            $modalInstance.close();
        }
    }]);
app.directive('ngCodeMirror1', [function () {

        return {
            restrict: 'EA',
            scope:{
                codeMirror: '='
            },
            link: function (scope, element, attrs) {
                var editor = $(element).find('.textarea')[0];
                //初始化在线代码编辑器
                var codeMirror = CodeMirror.fromTextArea(editor, {
                    theme: 'erlang-dark',
                    readOnly: true
                });
                scope.codeMirror = codeMirror;
            }
        }
    }]);
app.directive('ngCodeMirror2', [function () {

        return {
            restrict: 'EA',
            scope:{
                codeMirror: '='
            },
            link: function (scope, element, attrs) {
                var editor = $(element).find('.textarea')[0];
                //初始化在线代码编辑器
                var codeMirror = CodeMirror.fromTextArea(editor, {
                    theme: 'erlang-dark',
                    readOnly: true
                });
                scope.codeMirror = codeMirror;
            }
        }
    }]);
app.directive('ngCodeMirror6', [function () {

    return {
        restrict: 'EA',
        scope:{
            codeMirror: '='
        },
        link: function (scope, element, attrs) {
            var editor = $(element).find('.textarea')[0];
            //初始化在线代码编辑器
            var codeMirror = CodeMirror.fromTextArea(editor, {
                theme: 'erlang-dark',
                readOnly: true
            });
            scope.codeMirror = codeMirror;
        }
    }
}]);
app.directive('ngCodeMirror8', [function () {

        return {
            restrict: 'EA',
            scope:{
                codeMirror: '='
            },
            link: function (scope, element, attrs) {
                var editor = $(element).find('.textarea')[0];
                //初始化在线代码编辑器
                var codeMirror = CodeMirror.fromTextArea(editor, {
                    theme: 'erlang-dark',
                    // readOnly: true
                });
                scope.codeMirror = codeMirror;
            }
        }
    }]);
})();
